package com.zsrt.t10;

import java.util.Arrays;

/**
 * 请实现斗地主的算法。
 * 
 * 54 张牌 请告诉我3家各自的牌和河里的底牌
 * 
 * @author My
 *
 */
public class T10 {
	
	String[] num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
	String[] huaSe={"黑桃","红心","梅花","方块"};
	StringBuffer sb=new StringBuffer();
	String[] pai=new String[54];
	String[] wanjia=new String[17];
	
	
	/**
	 * 生成牌
	 */
	public  void pai(){
		
		for(int i=0;i<4;i++){
			for(int j=0;j<13;j++){
				String st=huaSe[i]+num[j]+",";
				sb.append(st);
			}
		}
		sb.append("大鬼,小鬼");
		pai=sb.toString().split(",");
	}
	
	/**
	 * 发牌
	 */
	public  void fenpai(){
		
		int c=0;
		for(int i=1;i<=3;i++){
			
			for(int j=1;j<=17;j++){
				
				//随机数发牌
				double random = Math.random();
				int floor =(int) Math.floor(random*(54-j+1-c));
				wanjia[j-1]=pai[floor];
				
				//删除已发的牌
				pai=delPai(pai[floor]);
			}
			
			//每发一个玩家打印一个玩家的牌
			System.out.println("玩家"+i+Arrays.toString(wanjia));
			c=c+17;
		}
		System.out.println("底牌"+Arrays.toString(pai));
	}
	
	/**
	 * 删除一张牌
	 * @param key 要删除的牌
	 * @return
	 */
	public String[] delPai(String key){
		
		//找要删除的牌的下标
		int xb=-1;
		for(int i=0;i<pai.length;i++){
			if(key.equals(pai[i])){
				xb=i;
			}
		}
		
		//新牌数组
		String[] newPai=new String[pai.length-1];
		for(int j=0;j<pai.length-1;j++){
			if(j<xb){
				newPai[j]=pai[j];
			}else if(j>=xb){
				newPai[j]=pai[j+1];
			}
		}
		
		return newPai;
	}
	

}
